﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using Facebook.Web;

namespace OnATous
{
    public partial class ContactNotation : System.Web.UI.Page
    {
        DBO.User user;
        DBO.User friend;
        DBO.Rating rate;
        List<DBO.User> friends;

        BusinessManagement.Feature featureBM;
        BusinessManagement.Facebook facebook;
        BusinessManagement.User userBM;
        BusinessManagement.Rating ratingBM;

        protected void Page_Load(object sender, EventArgs e)
        {
            facebook = new BusinessManagement.Facebook();
            userBM = new BusinessManagement.User();
            ratingBM = new BusinessManagement.Rating();
            featureBM = new BusinessManagement.Feature();

            if (!IsPostBack)
            {
                user = facebook.GetUser();
                friends = facebook.GetFriendList();


                userBM.UpdateUser(user);
                friend = userBM.GetFriendToEval(user, friends);
                rate = ratingBM.GetRate(user, friend);

                // Dans ce fichier, USER représente la personne connectée, CONTACT représente la personne à noter.
                numberofcredits.Text = user.Credits.ToString(); // Nombre de crédits restant
                numberofvotes.Text = ratingBM.GetNotationCount(user).ToString(); // Nombre de personnes ayant voté pour le user

                // Tu peux mettre ça dans une seule classe, c'est juste que moi ça me pétait des erreurs, comme j'ai pas la classe User
                Firstname.Text = friend.FirstName;
                Lastname.Text = friend.LastName;

                BigImage.ImageUrl = "https://graph.facebook.com/" + friend.Id + "/picture";

                switch (rate.Score)
                {
                    case 0: Vote.Text = Note0.ToolTip;
                        break;
                    case 1: Vote.Text = Note1.ToolTip;
                        break;
                    case 2: Vote.Text = Note2.ToolTip;
                        break;
                    case 3: Vote.Text = Note3.ToolTip;
                        break;
                    case 4: Vote.Text = Note4.ToolTip;
                        break;
                    default: Vote.Text = Note2.ToolTip;
                        break;
                }
                List<DBO.Feature> featureList = featureBM.GetFeatureList();

                ChoosePrincipal.DataSource = featureList;
                ChoosePrincipal.DataValueField = "Name";
                ChoosePrincipal.SelectedIndex = featureList.IndexOf(featureList.Where(x=>x.Id == rate.IdFeaturePrimary).FirstOrDefault());
                ChoosePrincipal.DataBind();
                ChooseSecondary.DataSource = featureList;
                ChooseSecondary.DataTextField = "Name";
                ChooseSecondary.SelectedIndex = featureList.IndexOf(featureList.Where(x => x.Id == rate.IdFeatureSecondary).FirstOrDefault());
                ChooseSecondary.DataBind();
                ChooseTertiary.DataSource = featureList;
                ChooseTertiary.DataTextField = "Name";
                ChooseTertiary.SelectedIndex = featureList.IndexOf(featureList.Where(x => x.Id == rate.IdFeatureTertiary).FirstOrDefault());
                ChooseTertiary.DataBind();

                // Pour chaque si non défini, mettre « ../Ressources/IMG/logo.jpg », sinon mettre ce qu'il y a.
                CaractPrincipale.ImageUrl = "../" + featureList[ChoosePrincipal.SelectedIndex].Icon;
                NameCaractPrincipale.Text = featureList[ChoosePrincipal.SelectedIndex].Name;
                CaractSecondaire.ImageUrl = "../" + featureList[ChooseSecondary.SelectedIndex].Icon;
                NameCaractSecondaire.Text = featureList[ChooseSecondary.SelectedIndex].Name;
                CaractTertiaire.ImageUrl = "../" + featureList[ChooseTertiary.SelectedIndex].Icon;
                NameCaractTertiaire.Text = featureList[ChooseTertiary.SelectedIndex].Name;

                if (rate.Comments == "") // Si rien n'a été défini
                    Commentaire.Text = "Laisser un commentaire…"; // Tu écris ça.
                else
                    Commentaire.Text = rate.Comments;

                Session["idUser"] = rate.IdUser;
                Session["idContact"] = rate.IdContact;
                Session["isFeaturePrimary"] = rate.IdFeaturePrimary;
                Session["isFeatureSecondary"] = rate.IdFeatureSecondary;
                Session["isFeatureTertiary"] = rate.IdFeatureTertiary;
                Session["score"] = rate.Score;
                Session["comments"] = rate.Comments;
                Session["isVisible"] = rate.IsVisible;
            }
        }

        protected void SaveVote(object sender, EventArgs e)
        {
            BusinessManagement.Rating ratingBM = new BusinessManagement.Rating();
            DBO.Rating rate = new DBO.Rating ((long) Session["idUser"], (long) Session["idContact"], (long) Session["isFeaturePrimary"], (long) Session["isFeatureSecondary"],
                 (long) Session["isFeatureTertiary"], (int) Session["score"], (string) Session["comments"], (bool) Session["isVisible"]);
            if (Commentaire.Text != "Laisser un commentaire…" && rate.Comments == "")
                rate.Comments = Commentaire.Text;
            List<DBO.Feature> featureList = featureBM.GetFeatureList();
            rate.IdFeaturePrimary = featureList[ChoosePrincipal.SelectedIndex].Id;
            rate.IdFeatureSecondary = featureList[ChooseSecondary.SelectedIndex].Id;
            rate.IdFeatureTertiary = featureList[ChooseTertiary.SelectedIndex].Id;
            // Sauvegarde les modifs dans la base
            bool res = ratingBM.ModifyRate(rate);
            CaractPrincipale.ImageUrl = "../" + featureList[ChoosePrincipal.SelectedIndex].Icon;
            NameCaractPrincipale.Text = featureList[ChoosePrincipal.SelectedIndex].Name;
            CaractSecondaire.ImageUrl = "../" + featureList[ChooseSecondary.SelectedIndex].Icon;
            NameCaractSecondaire.Text = featureList[ChooseSecondary.SelectedIndex].Name;
            CaractTertiaire.ImageUrl = "../" + featureList[ChooseTertiary.SelectedIndex].Icon;
            NameCaractTertiaire.Text = featureList[ChooseTertiary.SelectedIndex].Name;
        }

        protected void Set0(object sender, EventArgs e)
        {
            Session["score"] = 0;
            Vote.Text = Note0.ToolTip; // Ça, c'est juste pour tester, hein !
        }
        protected void Set1(object sender, EventArgs e)
        {
            Session["score"] = 1;
            Vote.Text = Note1.ToolTip; // Ça, c'est juste pour tester, hein !
        }
        protected void Set2(object sender, EventArgs e)
        {
            Session["score"] = 2;
            Vote.Text = Note2.ToolTip; // Ça, c'est juste pour tester, hein !
        }
        protected void Set3(object sender, EventArgs e)
        {
            Session["score"] = 3;
            Vote.Text = Note3.ToolTip; // Ça, c'est juste pour tester, hein !
        }
        protected void Set4(object sender, EventArgs e)
        {
            Session["score"] = 4;
            Vote.Text = Note4.ToolTip; // Ça, c'est juste pour tester, hein !
        }

        protected void setPrimary(object sender, EventArgs e)
        {
            CaractPrincipale.ImageUrl = "url de l'image";   // issue de choosePrincipal
            NameCaractPrincipale.Text = "nom de la caract"; // issue de choosePrincipal
        }
        protected void setSecondary(object sender, EventArgs e)
        {

        }
        protected void setTertary(object sender, EventArgs e)
        {

        }
    }
}